package com.jinyang.jetpackdemo.activity.ui;

import static android.widget.RelativeLayout.ALIGN_PARENT_LEFT;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.jinyang.jetpackdemo.R;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiuJinYang
 * @CreateDate: 2021/12/24
 * @Description:
 */
public class AvatarListLayout extends LinearLayout {
    private Context context;
    /**
     * 存放创建的最大的ImageView集合
     */
    private List<RoundedImageView> imageViewList=new ArrayList<>();
    /**
     * 默认图片大小
     */
    private int imageSize = Math.round(50);
    /**
     * 默认图片数量
     */
    private int imageMaxCount = 6;
    /**
     * 默认图片偏移百分比 0～1
     */
    private float imageOffset = 0.3f;

    public AvatarListLayout(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public AvatarListLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public AvatarListLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
//        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SDAvatarListLayout, defStyleAttr, 0);
//        imageMaxCount = ta.getInt(R.styleable.SDAvatarListLayout_image_max_count, imageMaxCount);
//        imageSize = (int) ta.getDimension(R.styleable.SDAvatarListLayout_image_size, imageSize);
//        imageOffset = ta.getFloat(R.styleable.SDAvatarListLayout_image_offset, imageOffset);
//        imageOffset = imageOffset > 1 ? 1 : imageOffset;
        init();
//        ta.recycle();
    }

    private void init() {
        setHorizontalScrollBarEnabled(false);
        RelativeLayout relativeLayout = new RelativeLayout(context);
        int offset = imageSize - (int) (imageSize * imageOffset);
        imageViewList = new ArrayList<>(imageMaxCount);
        for (int i = 0; i < imageMaxCount; i++) {
            RoundedImageView imageView = new RoundedImageView(context);
            imageView.setCornerRadius(50);
            imageView.setBorderColor(Color.parseColor("#ffff"));
            imageView.setBorderWidth(R.dimen.border_width);
            imageView.setId(imageView.hashCode() + i);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageSize, imageSize);
            params.addRule(ALIGN_PARENT_LEFT);
            params.setMargins((imageMaxCount - i - 1) * offset, 0, 0, 0);
            relativeLayout.addView(imageView, params);
            imageViewList.add(imageView);
        }
        relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        this.addView(relativeLayout);
    }

    public void setAvatarListListener(ShowAvatarListener listener) {
        hideAllImageView();
        listener.showImageView(imageViewList);
    }


    public void setAvatarListListener(List<String> drawableList) {
        if (drawableList == null) {
            return;
        }
        hideAllImageView();
        int i = imageMaxCount - 1;
        for (String url : drawableList) {
            GlideUtil.loadImg(context, url, imageViewList.get(i));
            imageViewList.get(i).setVisibility(VISIBLE);
            if (i == 0) {
                break;
            }
            --i;
        }
    }

    private void hideAllImageView() {
        for (RoundedImageView head : imageViewList) {
            head.setVisibility(View.GONE);
        }
    }

    public interface ShowAvatarListener {
        void showImageView(List<RoundedImageView> imageViewList);
    }
}